package ink.lovejinhu.linkedList.solve;

import ink.lovejinhu.common.ListNode;
import ink.lovejinhu.common.ProductList;
import org.junit.Test;

/**
 * @author jinhu
 * created 2021-02-04 15:24
 */
public class LinkedList_0001_ReverseList {
    /**
     * 链表逆置
     *
     * @param listNode
     * @return
     */
    public ListNode reverseList(ListNode listNode) {
        //新链表的头
        ListNode new_head = null;
        ListNode next = null;
        while (listNode != null) {
            next = listNode.next;
            listNode.next = new_head;
            new_head = listNode;
            listNode = next;
        }
        return new_head;
    }

    @Test
    public void test01() {
        ProductList productList = new ProductList();
        ListNode listNode = productList.creatListByLength(new int[]{1, 2, 3, 4});
        productList.printListNode(listNode);
        ListNode listNode1 = this.reverseList(listNode);
        productList.printListNode(listNode1);
    }
}
